public class BankDemo01 {
    public static void main(String[] args) {
//        NormalBank normalBank = new NormalBank("招商银行", 1000);
//        normalBank.saveMoney(200);
//        System.out.println(normalBank.getInfo());
//        normalBank.withDraw(1500);
//        System.out.println(normalBank.getInfo());
//        normalBank.withDraw(800);
//        System.out.println(normalBank.getInfo());

        NormalBank cb = new CreditBank("招商银行", 1000, 2000);
        cb.saveMoney(500);// 存
        System.out.println(cb.getInfo());

        cb.withDraw(1000);
        System.out.println(cb.getInfo());

        cb.withDraw(1000);
        System.out.println(cb.getInfo());

        cb.saveMoney(1000);
        System.out.println(cb.getInfo());


    }
}


class NormalBank {
    private String name;
    private float balance;// 余额

    public NormalBank(String name, float balance) {
        this.name = name;
        this.balance = balance;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getBalance() {
        return balance;
    }

    public void setBalance(float balance) {
        this.balance = balance;
    }

    // 取钱
    public void withDraw(float money) {
        if (money < 0) {
            System.out.println("取款不能为负数");
            return;
        }
        if (money > balance) {
            System.out.println("余额不足");
            return;
        }
        balance -= money;
    }

    // 存钱
    public void saveMoney(float money) {
        if (money < 0) {
            System.out.println("存款不能为负数");
            return;
        }
        balance += money;
    }

    //获取信息的方法
    public String getInfo() {
        return "银行名字：" + getName() + "余额：" + getBalance();
    }
}

class CreditBank extends NormalBank {
    private float allQuota;// 总额度
    private float useQuota;// 已用额度

    public CreditBank(String name, float balance) {
        super(name, balance);
    }

    public CreditBank(String name, float balance, float allQuota) {
        super(name, balance);
        this.allQuota = allQuota;
    }

    @Override
    public void withDraw(float money) {
        if (money < 0) {
            System.out.println("取款金额不能为负数");
            return;
        }
        // 取的钱大于余额+剩余额度
        if (money > getBalance() + (allQuota - useQuota)) {
            System.out.println("余额不足");
            return;
        }
        if (money <= getBalance()) {
            super.withDraw(money);
            return;
        }

        if (money > getBalance()) {
            useQuota += money - getBalance();
            super.withDraw(getBalance());
        }
    }

    @Override
    public void saveMoney(float money) {
        if (money < 0) {
            System.out.println("存款金额不能为负数");
            return;
        }
        if (useQuota == 0) {
            super.saveMoney(money);
            return;
        }
        // 如果还的钱大于等于使用额度，把额度清空，把剩余存在的储蓄卡当中
        if (money >= useQuota) {
            super.saveMoney(money - useQuota);
            this.useQuota = 0;
        }
    }

    @Override
    public String getInfo() {
        return super.getInfo() + "总透支额度:" + this.allQuota + "已透支额度:" + this.useQuota;
    }
}
